using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Acms.SqlDal
{
    public class UserRoleDal : Base
    {
        public enum Fields
        {
		    Id, 
		    UserId,
		    UserName,
		    FirstName,
		    LastName,
		    RoleId,
		    RoleName,
            IsAdminRole,
            SiteId,
		    Created,
		    Start,
		    End
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.UserName, reader, table);
            GetReaderValue<string>(Fields.FirstName, reader, table);
            GetReaderValue<string>(Fields.LastName, reader, table);
            GetReaderValue<int>(Fields.RoleId, reader, table);
            GetReaderValue<string>(Fields.RoleName, reader, table);
            GetReaderValue<bool>(Fields.IsAdminRole, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
            GetReaderValue<DateTime>(Fields.Start, reader, table);
            GetReaderValue<DateTime?>(Fields.End, reader, table);
        }

        public void Update(ref int id, int userId, ref string userName, ref string firstName, ref string lastName, int roleId, ref string roleName, ref bool isAdminRole, int siteId, DateTime created, DateTime start, DateTime? end, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int); 
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int); 
            AddInputParameter(cmd, "RoleId", roleId, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime); 
            AddInputParameter(cmd, "Start", start, SqlDbType.DateTime); 
            AddInputParameter(cmd, "End", end, SqlDbType.DateTime); 
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "UserNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "FirstNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "LastNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "RoleNameOut", SqlDbType.VarChar, 100);
            AddOutputParameter(cmd, "IsAdminRoleOut", SqlDbType.Bit);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            userName = GetOutputValue<string>(cmd, "UserNameOut");
            firstName = GetOutputValue<string>(cmd, "FirstNameOut");
            lastName = GetOutputValue<string>(cmd, "LastNameOut");
            roleName = GetOutputValue<string>(cmd, "RoleNameOut");
            isAdminRole = GetOutputValue<bool>(cmd, "IsAdminRoleOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }


        public Hashtable[] GetAllInactiveByUserIdSiteId(int userId, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllInactiveByUserIdSiteId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllActiveByUserIdSiteId(int userId, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllActiveByUserIdSiteId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByUserIdSiteIdAndTime(int userId, int siteId, DateTime dateTime)
        {
            SqlCommand cmd = CreateCommand("GetAllByUserIdSiteIdAndTime");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "DateTime", dateTime, SqlDbType.DateTime);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }
    }
}
